$(function () {
    initData()
});

/**
 *
 */
function initData() {
    var userType = $("#userType").val();
    console.log('userType', userType);
    if(userType == 2){//集团
        getTownRecordTypeGroup()
    }else if(userType == 3){//片区
        getTownRecordTypePianqu()
    }else if(userType == 4 ||userType == 5 ||userType == 6 || userType == 7 ||userType == 10){
        getTownRecordTypeShop()
    }
}

/**
 *  获取集团的数据
 * @param type
 */
function getTownRecordTypeGroup(type) {
    $.ajax({
        type: "POST",
        url: ctx + "module/pianqu/getPianquDeviceTypeChart",
        data: {
            type:type
        },
        success: function (data) {
            if (data.code == 0) {
                initTownRecordTypeChart(data)
                initDeviceWenduChart(data);
            }
        }
    });
}


/**
 *  获取片区的数据
 * @param type
 */
function getTownRecordTypePianqu(type) {
    $.ajax({
        type: "POST",
        url: ctx + "module/shop/getShopListByPianquChart",
        data: {
            type:type
        },
        success: function (data) {
            if (data.code == 0) {
                initTownRecordTypeChart(data)
                initDeviceWenduChart(data);
            }
        }
    });
}


/**
 *  获取企业的数据
 * @param type
 */
function getTownRecordTypeShop(type) {
    $.ajax({
        type: "POST",
        url: ctx + "module/shop/getShopListByChart",
        data: {
            type:type
        },
        success: function (data) {
            if (data.code == 0) {
                initTownRecordTypeChart(data);
                initDeviceWenduChart(data);
            }
        }
    });
}

function initTownRecordTypeChart(data){
    console.log('data',data)

    var chart = echarts.init(document.getElementById('town_chart'));
    var option3 = {
        color: ['#df5353','#50fae9' ],
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            // y:'b ottom',
            data: [{name: '普通电焊机'}, {name: '等离子焊机'}],
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        calculable: true,
        xAxis: [
            {
                type: 'category',
                data: data.pianquName,
                axisLabel:{
                    interval:0
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                minInterval:1,
                splitLine: {
                    show: false
                },
                interval: 1, //设置y轴刻度间隔
            }
        ],
        series: [
            {
                label: {
                    show: true,
                    position: 'top'
                },
                name: '普通电焊机',
                type: 'bar',
                data: data.device0Count,
            },
            {
                label: {
                    show: true,
                    position: 'top'
                },
                name: '等离子焊机',
                type: 'bar',
                data: data.device1Count,
            }
        ]
    };
    chart.setOption(option3);
    $(window).resize(chart.resize);
    // chart.on("click", loadEvent);
}



function initDeviceWenduChart(data){
    console.log('data',data)

    let centerTxt = "";

    let countAll = 0;
    if(data.tempDeviceList!= undefined){
        for (let i = 0; i < data.tempDeviceList.length; i++) {
            countAll += data.tempDeviceList[i].value;
        }


        var chart = echarts.init(document.getElementById('device_chart'));
        var option3 = {
            tooltip: {
                trigger: 'item'
            },
            title: {
                text: '制粒机测温',
                subtext: '总数: ' + countAll,
                left: 'center'
            },
            color:['#1374c7','#FFBF00','#4eef35',  ],
            legend: {
                orient: 'vertical',
                left: 'left',
                formatter:(name) => {
                    console.log('legend name',name)
                    let target;
                    const dataItem = data.tempDeviceList;
                    for (let i = 0; i < dataItem.length; i++) {
                        if (dataItem[i].name === name) {
                            target = `${dataItem[i].value + ' ' + '台'} `;
                        }
                    }
                    return name + ' ' + target;
                },
            },
            series: [
                {
                    name: '制粒机设备数',
                    type: 'pie',
                    radius: ['40%', '70%'],
                    avoidLabelOverlap: false,
                    // itemStyle: {
                    //     borderRadius: 10,
                    //     borderColor: '#fff',
                    //     borderWidth: 2
                    // },
                    label: {
                        alignTo: 'edge',
                        formatter: '{name|{b}}\n{value|{c} 台}',
                        minMargin: 5,
                        edgeDistance: '15%',
                        lineHeight: 20,
                        rich: {
                            time: {
                                fontSize: 10,
                                color: '#999'
                            }
                        },
                    },
                    labelLine: {
                        length: 10,
                        length2: 0,
                        maxSurfaceAngle: 80
                    },
                    labelLayout: function (params) {
                        const isLeft = params.labelRect.x < chart.getWidth() / 2;
                        const points = params.labelLinePoints;
                        console.log('points',points)
                        // Update the end point.
                        points[2][0] = isLeft
                            ? params.labelRect.x
                            : params.labelRect.x + params.labelRect.width;
                        return {
                            labelLinePoints: points
                        };
                    },
                    emphasis: {
                        label: {
                            show: true,
                            fontSize: 40,
                            fontWeight: 'bold',
                        },
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    },
                    data: data.tempDeviceList
                }
            ]
        };
        chart.setOption(option3);
        $(window).resize(chart.resize);
        chart.on("click", loadEvent);
    }


}

function loadEvent(e){
    console.log(e.data)
    // 索引  e.dataIndex
    goShopList(e.data.limitId)
}



function goShopList(limitId){
    // var index = layer.open({
    //     type : 2,
    //     title : '企业列表',
    //     maxmin : true,
    //     shadeClose : false, // 点击遮罩关闭层
    //     area : [ '600px', '420px' ],
    //     content :ctx + 'module/shop/tempValueList/' + valueIndex // iframe的url
    // });
    // layer.full(index);
    window.location.href = ctx + 'module/shop/tempValueList/' + limitId
}
